﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ShakeIt_.ServiceReference2;

namespace ShakeIt_
{
    public partial class userInfoPage : PhoneApplicationPage
    {
        public userInfoPage()
        {
            InitializeComponent();
            /* 1.显示详细信息 名字：Data.userFocusedName / 手机：Data.userFocusedPhone / 平均得分：Data.scoreResearch 【得分的修改在proxy_GetAllCommentCompleted内部】
             * 2.if( focus == true ) 显示 "已关注" 和 <取消关注> 按钮，点击触发函数 Data.proxy.RemoveFocusAsync(Data.userFocusedId, Data.userId);
             *    else if( focus == false ) 显示 "未关注" 和 <关注> 按钮，点击触发函数 Data.proxy.RemoveFocusAsync(Data.userFocusedId, Data.userId);
             * 3.按钮 <TA的评分> 操作
             *    调用 Data.proxy.GetAllCommentCompleted += new EventHandler<GetAllCommentCompletedEventArgs>(proxy_GetAllCommentCompleted);
             *       和 Data.proxy.GetAllCommentAsync(Data.userFocusedId);
             *    跳转到 commentPage.xaml
             * 4.按钮 <给TA发纸条> 操作
             *     4.1 Data.userFocusedId = <目标>.id
             *     4.2 Data.userFocusedName = <目标>.username
             *     4.3 跳转到 sendMessagePage.xaml
             */
            Data.proxy.IsFocusCompleted +=new EventHandler<IsFocusCompletedEventArgs>(IsFocusCompleted);
            Data.proxy.RemoveFocusCompleted += new EventHandler<RemoveFocusCompletedEventArgs>(RemoveFocusCompleted);
            Data.proxy.SetFocusCompleted += new EventHandler<SetFocusCompletedEventArgs>(SetFocusCompleted);
            Data.proxy.GetAllCommentCompleted += new EventHandler<GetAllCommentCompletedEventArgs>(proxy_GetAllCommentCompleted);
            Data.proxy.GetAllCommentAsync(Data.userFocusedId);
        }

        private void proxy_GetAllCommentCompleted(object sender, GetAllCommentCompletedEventArgs e)
        {
            Data.calculateScore(Data.userFocusedName);
            textBlock4.Text = Data.scoreResearch.ToString();
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            PageTitle.Text = Data.userFocusedName;
            textBlock2.Text = Data.userFocusedPhone;

            refreshFocusButton(Data.focus);
        }

        private void refreshFocusButton(bool focus)
        {
            if (focus)
            {
                button1.Content = "取消关注";
            }
            else
            {
                button1.Content = "关注";
            }
        }

        private void RemoveFocusCompleted(object sender, RemoveFocusCompletedEventArgs e)
        {
            if (e.Result == "成功移除关注")
            {
                Data.focus = false;
                button1.Content = "关注";
            }
        }

        private void SetFocusCompleted(object sender, SetFocusCompletedEventArgs e)
        {
            if (e.Result == "成功添加关注")
            {
                Data.focus = true;
                button1.Content = "取消关注";
            }
        }

        private void IsFocusCompleted(object sender, IsFocusCompletedEventArgs e)
        {
            refreshFocusButton(e.Result);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (Data.focus)
            {
                Data.proxy.RemoveFocusAsync(Data.userFocusedId, Data.userId);
            }
            else
            {
                Data.proxy.SetFocusAsync(Data.userFocusedId, Data.userId);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/commentPage.xaml", UriKind.Relative));
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text != "")
            {
                Data.proxy.InsertChatAsync(textBox1.Text, Data.userId, Data.userFocusedId);
            }
        }
        /* 1.标题：发纸条
             * 2.目标：Data.userFocusedName
             * 3.内容：一个输入框
             * 4.按钮 <发送> 调用函数 Data.proxy.InsertChatAsync(content, Data.userId, Data.userFocusedId);
             */
    }
}